<?php
/**
 * Created by PhpStorm.
 * User: hda
 * Date: 2017/6/1
 * Time: 上午9:21
 */

namespace logic\wechat\index;

use model\ShopUserAddress;
use think\Db;

class LUserAddress
{
    private $model;

    function __construct()
    {
        $this->model = new ShopUserAddress();
    }

    public function select($account_id, $field = null)
    {
        $where = [
            'account_id' => $account_id,
            'flag_delete' => 0
        ];
        $data = $this->model->where($where)->field($field)->order('id', 'desc')->select();
        return $data;
    }

    public function find($address_id, $account_id)
    {
        $where = [
            'id' => $address_id,
            'account_id' => $account_id,
            'flag_delete' => 0
        ];
        $data = $this->model->where($where)->find();
        return $data;
    }

    public function post($addres_id, $post_data, $account_id)
    {
        if ($addres_id) {
            //修改
            $a = $this->model->where(['id' => $addres_id, 'account_id' => $account_id])->update($post_data);
            if ($a !== false) {
                $result = [
                    'flag' => true,
                    'msg' => '修改成功'
                ];
            } else {
                $result = [
                    'flag' => false,
                    'msg' => $this->model->getError()
                ];
            }
        } else {
            //新增
            $count = $this->model->where(['account_id' => $account_id, 'flag_delete' => 0])->count('id');
            if ($count == 0) {
                $post_data['flag_default'] = 1;
            }
            $b = $this->model->insertGetId($post_data);
            if ($b > 0) {
                $result = [
                    'flag' => true,
                    'msg' => '增加成功'
                ];
            } else {
                $result = [
                    'flag' => false,
                    'msg' => $this->model->getError()
                ];
            }
        }
        return $result;

    }

    public function set_default($address_id, $account_id)
    {
        if ($address_id) {
            Db::startTrans();

            $a = $this->model->where(['account_id' => $account_id, 'flag_default' => 1, 'flag_delete' => 0])->update(['flag_default' => 0]);
            if ($a !== false) {
                $b = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->update(['flag_default' => 1]);
                if ($b !== false) {
                    Db::commit();
                    return ['flag' => true, 'msg' => '操作成功'];
                } else {
                    Db::rollback();
                    return ['flag' => false, 'msg' => '操作失败'];
                }
            } else {
                Db::rollback();
                return ['flag' => false, 'msg' => '操作失败'];
            }

        } else {
            return ['flag' => false, 'msg' => '地址异常'];
        }
    }

    public function delete($address_id, $account_id)
    {
        if ($address_id) {
            $m = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->find();
            $list = $this->model->where('id', '<>', $address_id)->where(['account_id' => $account_id, 'flag_delete' => 0])->order(['id' => 'desc'])->select();
            if ($m['flag_default'] && count($list) > 1) {
                //删除默认地址，要自动设置另一个给他
                Db::startTrans();
//                $a = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->update(['flag_default' => 0, 'flag_delete' => 1]);
                $a = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->delete();
                if ($a !== false) {
                    foreach ($list as $address) {
                        $b = $this->model->where(['id' => $address['id'], 'account_id' => $account_id])->update(['flag_default' => 1]);
                        if ($b !== false) {
                            Db::commit();
                            return ['flag' => true, 'msg' => '删除成功','address' => $address];
                        } else {
                            Db::rollback();
                            return ['flag' => false, 'msg' => '操作失败'];
                        }
                    }
                } else {
                    Db::rollback();
                    return ['flag' => false, 'msg' => '操作失败'];
                }
            } else {
//                $c = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->update(['flag_delete' => 1]);
                $c = $this->model->where(['id' => $address_id, 'account_id' => $account_id])->delete();
                if ($c !== false) {
                    return ['flag' => true, 'msg' => '删除成功'];
                } else {
                    return ['flag' => false, 'msg' => '操作失败'];
                }
            }
        } else {
            return ['flag' => false, 'msg' => '地址异常'];
        }
    }

}